﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VsSpeakerCompanion;
using TwitterAPI;

namespace PeopleCounterWidget
{
    /// <summary>
    /// Keeps count of the total number of unique people participating in the audience
    /// </summary>
    class PeopleCounterWidget : IWidget
    {
        private TwitterService twitterService;
        private PeopleCounter counter;

        public PeopleCounterWidget(TwitterService twitterService)
        {
            this.twitterService = twitterService;

            // listen to when a new user is detected
            this.twitterService.UsersCountChanged += new EventHandler<UsersCountEventArgs>(twitterService_UsersCountChanged);

            this.counter = new PeopleCounter();
        }

        private void twitterService_UsersCountChanged(object source, UsersCountEventArgs e)
        {
            // update our UI to show the new people count
            counter.Dispatcher.Invoke(new Action(() =>  counter.Number = e.NewUsersCount));
        }

        public void Reset()
        {
            // reset the count of people back to zero
            counter.Dispatcher.Invoke(new Action(() => counter.Number = 0 ));
        }

        public System.Windows.UIElement VisualElement
        {
            // this is our UI
            get { return counter; }
        }
    }
}
